Aplicações de séries de Fourier à compressão de áudio

In [70]:
using FFTW
using WAV
using DSP
using SampledSignals
using Plots
In [3]:
audio_one_channel = SampleBuf(rand(100000), 10000)
Out[3]:
In [4]:
audio_two_channel = SampleBuf(rand(100000,2), 10000)
Out[4]:
In [5]:
audio_multi_channel = SampleBuf(rand(100000,2), 10000)
Out[5]:
In [9]:
typeof(audio_one_channel.data)
Out[9]:
Vector{Float64} (alias for Array{Float64, 1})
In [12]:
sizeof(audio_one_channel)
Out[12]:
16
In [13]:
size(audio_one_channel)
Out[13]:
(100000,)
In [10]:
sizeof(audio_one_channel.data)
Out[10]:
800000
In [15]:
audio_one_channel.samplerate
Out[15]:
10000.0
In [16]:
squarewave(x::Real) = ifelse(mod2pi(x) < π, 1.0, -1.0)
Out[16]:
squarewave (generic function with 1 method)
In [17]:
function squarewave(x::Real, θ::Real)
    0  θ  1 || throw(DomainError(θ, "squwarewave(x, θ) is only defined for 0 ≤ θ ≤ 1."))
    ifelse(mod2pi(x) < 2π * θ, 1.0, -1.0)
end
Out[17]:
squarewave (generic function with 2 methods)
In [18]:
function trianglewave(x::Real)
    modx = mod2pi(x + π/2)
    ifelse(modx < π, 2modx/π - 1, -2modx/π + 3)
end
Out[18]:
trianglewave (generic function with 1 method)
In [19]:
sawtoothwave(x::Real) = rem2pi(x, RoundNearest) / π
Out[19]:
sawtoothwave (generic function with 1 method)
In [24]:
plot([squarewave], -2π, 2π)
Out[24]:
In [25]:
plot([sin], -2π, 2π)
Out[25]:
In [26]:
plot([sawtoothwave], -2π, 2π)
Out[26]:
In [28]:
plot([trianglewave], -2π, 2π)
Out[28]:
In [29]:
A = .8; phi = π/2; f0= 1000; fs = 44100;
In [42]:
t = -0.002:1/fs:10*0.002
y = A*cos.(2*pi*f0*t .+ phi)
plot(t, y)
Out[42]:
In [43]:
wavplay(y, fs)
In [53]:
A1 = .8; A2 = .8; phi1 = π/2; phi2 = π/2; 
f01 = 1000; f02 = 1000; fst = 44100;
In [54]:
tr = -0.002:1/fs:0.002
y1 = A1*sin.(2*pi*f01*tr .+ phi1)
y2 = A2*cos.(2*pi*f02*tr .+ phi2)
addresult =y1 .+ y2
subresult =y1 .- y2
mulresult =y1 .* y2
divresult =y1 ./ y2
wavplay(subresult, fst)
In [55]:
plot(tr, y1)
Out[55]:
In [56]:
plot(tr, y2)
Out[56]:
In [57]:
plot(tr, addresult)
Out[57]:
In [58]:
plot(tr, subresult)
Out[58]:
In [59]:
plot(tr, mulresult)
Out[59]:
In [60]:
plot(tr, divresult)
Out[60]:
In [62]:
wave1, fs1 = wavread(joinpath("data", "piano-phrase.wav"))
In [90]:
wavplay(wave1, fs1)
In [91]:
fs1
Out[91]:
44100.0f0
In [65]:
Az = .8; phiz = π/2; fsd = 44100;
In [66]:
tw = 0.0:1/fsd:(length(wave1)-1)/fsd
plot(tw, wave1[:,1])
Out[66]:
In [71]:
plot(abs.(fft(wave1)).^2)
Out[71]:
In [72]:
plot(tw[1:div(3end,16)], wave1[1:div(3end,16),1])
Out[72]:
In [73]:
plot(tw[2000:35000], wave1[2000:35000,1])
Out[73]:
In [96]:
wavplay(wave1[2000:35000,1], fs1)
In [100]:
plot(abs.(fft(wave1[2000:35000,1])).^2)
Out[100]:
In [ ]:

In [77]:
plot(0:1/fs1:(length(wave1)-1)/fs1, wave1, xlabel= "Time [s]")
Out[77]:

Outra parte

In [78]:
Az = .8; phiz = π/2; fsd = 44100;
In [80]:
f0z = 880

tz = 1:1/fsd:11
wavez = Az./tz.*sin.(2*pi*f0z*tz .+ phiz);
In [81]:
wavplay(wavez, fsd)
In [82]:
f1 = 200; f2= 400; f3 = 600; f4 = 800; f5 = 1000;
In [84]:
ta = -0.005:1/fsd:0.005
harmonic1 = A*sin.(2*pi*f1*ta .+ phi)
harmonic2 = A*sin.(2*pi*f2*ta .+ phi)
harmonic3 = A*sin.(2*pi*f3*ta .+ phi)
harmonic4 = A*sin.(2*pi*f4*ta .+ phi)
harmonic5 = A*sin.(2*pi*f5*ta .+ phi)
nothing
In [85]:
wavplay(harmonic5, fsd)
In [86]:
composite = harmonic1 .+ harmonic2 .+ harmonic3 .+ harmonic4 .+harmonic5
nothing
In [87]:
plot(ta,composite)
Out[87]:
In [89]:
wavplay(composite, fsd)
In [ ]: